/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.lidovky.articles.articlesData.pesta;

import org.joda.time.IllegalFieldValueException;
import org.joda.time.LocalDate;

/**
 *
 * @author x211800
 */
public class ColumnOfDates extends Columns {
    private static final String ZERO = "0";
    private int countOfDates = 0;
    private String volumeNumber = null;
    private static ColumnOfDates instance = null;
    private String parsedDates = null;

    private ColumnOfDates() {
    }


    public static ColumnOfDates instanceOf(String columnContent) throws ColumnsError{

        if (instance == null){
            instance = new ColumnOfDates();
        }
        instance.parseColumn(columnContent);
        return instance;
    }


/**
 *
 * @param columns[5]
 * @return String{parsed dates in format yyyy, mm, dd}
 * @throws ColumnsError{see message}
 */
    private void parseColumn(String column) throws ColumnsError {
        String date;
        String unparsedDate;
        countOfDates = 0;
        volumeNumber = null;


        if (column.contains(String.valueOf(CSV_TEXT_SEPARATOR))) {
            unparsedDate = removeWhiteSpaces(removeTextDelimits(column));
        } else {
            unparsedDate = removeWhiteSpaces(column);
        }

        if (unparsedDate.contains(COMA)) {

            String[] dates = unparsedDate.split(COMA);
            StringBuilder dateBuilder = new StringBuilder("");

            for (int i = 0; i < dates.length; i++) {
                if (i != 0) {
                    dateBuilder.append(",");
                }
                String resultDate = parseDate(dates[i]);
                if (resultDate == null) {
                    throw new ColumnsError("null String[] date problem");

                } else {
                    dateBuilder.append(resultDate);
                }
            }
            date = dateBuilder.toString();

        } else {

            date = parseDate(unparsedDate);
            if (date == null) {
                throw new ColumnsError("null String[] date problem");
            }
        }
        parsedDates = date;
    }

    /**
     *
     * @return String[]{yyyy, mm, dd}
     */
    private String parseDate(String unparsedDate) throws ColumnsError {
        String[] splitedOriginDate;
        String lastVolumeNumber = volumeNumber;
        volumeNumber = null;

        if (unparsedDate.contains(SLASH)) {
            splitedOriginDate = unparsedDate.split(SLASH);

            if (splitedOriginDate.length != 3) {
                return null;
            }

            if (lastVolumeNumber != null && !lastVolumeNumber.equals(String.valueOf(parseVolumeNumber(Integer.valueOf(splitedOriginDate[2]))))) {
                throw new ColumnsError("multiple volume number problem");
            } else {
                volumeNumber = String.valueOf(parseVolumeNumber(Integer.valueOf(splitedOriginDate[2])));
            }
            countOfDates++;
            controlDate(splitedOriginDate[2] + addZero(splitedOriginDate[0]) + addZero(splitedOriginDate[1]));
            return splitedOriginDate[2] + addZero(splitedOriginDate[0]) + addZero(splitedOriginDate[1]);

        } else if (unparsedDate.contains(DOT)) {
            splitedOriginDate = unparsedDate.split(String.valueOf("\\."));

            if (splitedOriginDate.length != 3) {
                return null;
            }

            if (lastVolumeNumber != null && !lastVolumeNumber.equals(String.valueOf(parseVolumeNumber(Integer.valueOf(splitedOriginDate[2]))))) {
                throw new ColumnsError("multiple volume number problem " + unparsedDate + " origin volume number: " + volumeNumber + " current volume number " + String.valueOf(parseVolumeNumber(Integer.valueOf(splitedOriginDate[2]))));
            } else {
                volumeNumber = String.valueOf(parseVolumeNumber(Integer.valueOf(splitedOriginDate[2])));
            }
            countOfDates++;
            controlDate(splitedOriginDate[2] + addZero(splitedOriginDate[1]) + addZero(splitedOriginDate[0]));
            return splitedOriginDate[2] + addZero(splitedOriginDate[1]) + addZero(splitedOriginDate[0]);
        }
        return null;
    }


    private String addZero(String date) {
        if (date.length() == 1) {
            return ZERO + date;
        }
        return date;
    }

    private void controlDate(String date) throws ColumnsError {
        try {
            String yearStr = date.substring(0, 4);
            int year = Integer.parseInt(yearStr);
            String monthStr = date.substring(4, 6);
            int month = Integer.parseInt(monthStr);
            String dayStr = date.substring(6, 8);
            int day = Integer.parseInt(dayStr);

            LocalDate ld = new LocalDate(year, month, day);

        } catch (IllegalFieldValueException ex) {
            throw new ColumnsError("problem with creating LocalDate");
        }
    }

    private int parseVolumeNumber(int year) throws ColumnsError {
        if (year >= 1893 && year <= 1943) {
            return year - 1892;
        } else {
            throw new ColumnsError("problem with parsing year and volume number");
        }
    }

    public int getCountOfDates() {
        return countOfDates;
    }


    public String getVolumeNumber() {
        return volumeNumber;
    }

    public String getParsedDates() {
        return parsedDates;
    }


}
